package org.wombat.service.view; /**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 07-11-2010
 * Time: 12:26:14
 * To change this template use File | Settings | File Templates.
 */



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.wombat.model.User;
import org.wombat.service.IUserService;
import org.wombat.util.LoggerNames;

import javax.annotation.Resource;

@Controller
public class IndexController {
    Logger logNormal = LoggerFactory.getLogger(LoggerNames.LOGGER_PRESENTATION);
    Logger logError = LoggerFactory.getLogger(LoggerNames.LOGGER_ERROR);
    IUserService userService;

    @Required
    @Resource(name="userService")
    public void setUserService(IUserService userService) {this.userService = userService;}

    /**
     * For every request for this controller, this will
     * create a person instance for the form.
     */    
    public @ModelAttribute("person") User newRequest() {
        return new User("","");
    }

    @RequestMapping("/index.html")
    public String showIndexPageWithUser(@ModelAttribute("person") User person, Model model) {
        if( ! person.getPassword().isEmpty() && ! person.getUserName().isEmpty() ){
            boolean userExists = userService.getUser(person.getUserName()) != null;
            if(userExists){
                model.addAttribute("person",userService.getUser(person.getUserName()));
            }
        }

        return "index";
    }
}
